<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>连板</title>
		<link href="css/text.css" rel="stylesheet" type="text/css" />
		<style type="text/css">						
		  .fixedBox{
				border: 1px solid #555555;				
				white-space: nowrap;	
			}
			.fixedBox::-webkit-scrollbar {
				width: 12px;     
				height: 12px;				
			}
			table{
				width:80px; 
				border-collapse:collapse; 
				text-align:center;
			}
			td,th{border:1px solid #696969}				
		</style>
	</head>
	<body>		
		<div id="towriter" class="fixedBox" style="margin-left:-20px;display:none;">
			<div id="towrite" class="fixedBox" style="position:relative; float:left;margin-top:-20px;">
				<div id="title" style="margin:10px 50px -5px 5px;"></div>
				<div id="towrites" class="fixedBox" style="position:relative; float:left;"></div>	
				<div id="towriteb" style="float:left;margin-left:-10px;">
					<div id="towrite1" ></div>									
					<div id="towrite2"></div>
				</div>
			</div>	
		</div>
		
		<script type="text/javascript" src="js/json2html.js"></script>
		<script type="text/javascript" src="js/jquery.min.js"></script>
		<script type="text/javascript" src="DatePicker/WdatePicker.js"> </script>
		<script>
			var bkcolor = $("body").css("backgroundColor")
			var dt = "";
			var tt = "";
			var gt = "";
			var k = "0";
			window.onload = function() {
			var day = new Date();
			gt = day.getTime();
			mymonth = day.getMonth()+1;
			myday = day.getDate();
			if(mymonth >= 10){mymonth = mymonth;}else{mymonth = "0" + mymonth;}
			if(myday >= 10){myday = myday;}else{myday = "0" + myday;}
			tt = day.getFullYear() + "-" + mymonth + "-" + myday;	
			dt = tt;
			changeWith();	//宽度变化	
			changeStyle();
			start();
			
			}
						
			function time_range(beginTime, endTime) {
				var strb = beginTime.split(":");
				if (strb.length != 2) {
					return false;
				}

				var stre = endTime.split(":");
				if (stre.length != 2) {
					return false;
				}

				var b = new Date();
				var e = new Date();
				var n = new Date();

				b.setHours(strb[0]);
				b.setMinutes(strb[1]);
				e.setHours(stre[0]);
				e.setMinutes(stre[1]);

				if (n.getTime() - b.getTime() > 0 && n.getTime() - e.getTime() < 0) {
					start();					
					console.log(true)　　　　　　
					return true;				
				} else {
					console.log(false)
				    return false;     
				}
			}　　　
   　　　
			$(function() {
				setInterval(function() {
					if(k == "0"){
						time_range("9:00","15:15")	
					}									
				},
				60000);
			})
			
			var w = 0;  
			function changeWith() {
				var pr = screen.availWidth / window.outerWidth;				
				w = 1600 * pr;	//根据显示数量，微调此值			
				var obja = document.getElementById("towrite");				
				obja.style.left= "0px"; 				
				obja.style.width= w + "px";	
				//obja.style.height= "450px";
			}
			
			window.onresize = function(){
				changeWith();	//宽度变化
			}
			
			function changeStyle() {				
				var obj = document.getElementById("towriter");
				obj.style.display= "block";	
				obj.style.left= "0px"; 				
				obj.style.width= "1350px";													
				
				var obja = document.getElementById("towrites");  //首板
				obja.style.left= "0px"; 				
				obja.style.width= "280px";							
				obja.style.fontSize="12px";	
				obja.style.height= "500px";
				
				var div = document.getElementById("towrite1");
				for (i = 1; i < 5; i++){
					var obj = document.createElement("div");
					obj.id = "towritea" + i;
					obj.className= "fixedBox";
					obj.style.position= "relative"; 
					obj.style.float= "left"; 
					obj.style.left= "0px"; 				
					obj.style.width= "280px";
					obj.style.height= "220px";					
					obj.style.fontSize="12px";	
					//obj.style.border = "1px solid #fff";
					div.appendChild(obj);					
				}
				
				var div = document.getElementById("towrite2");
				for (i = 1; i < 5; i++){
					var obj = document.createElement("div");
					obj.id = "towriteb" + i;
					obj.className= "fixedBox";
					obj.style.position= "relative"; 
					obj.style.float= "left"; 
					obj.style.left= "0px"; 				
					obj.style.width= "280px";
					obj.style.height= "260px";					
					obj.style.fontSize="12px";	
					div.appendChild(obj);					
				}				
			}
			
			Date.prototype.Format = function (fmt) {
			var o = {
				"M+": this.getMonth() + 1, //月份
				"d+": this.getDate(), //日
				"h+": this.getHours(), //小时
				"m+": this.getMinutes(), //分
				"s+": this.getSeconds(), //秒
				"q+": Math.floor((this.getMonth() + 3) / 3), //季度
				"S": this.getMilliseconds() //毫秒
			};
			
			if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
				for (var k in o)
				if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
				return fmt;
			};
			
			function getLocalTime(nS) {			          
			return new Date(parseInt(nS) * 1000).Format("hh:mm:ss")}
			
			function keepTwoDecimal(num) {  
               var result = parseFloat(num);  
               if (isNaN(result)) {                    
                   return false;  
               }  
               result = Math.round(num * 100) / 100;  
               return result;  
			};
			
			function NumberTransform(num) { 
				var result = num;
               if(Math.abs(num) > 100000000){
					result = num / 100000000;
					result = result.toFixed(2) + "亿";
			   }else if(Math.abs(num) > 10000){
					result = num/10000;
					result = result.toFixed(0) + "万";
			   }				   
               return result;  
			};	
			
			function cDayFunc(dp) 
			{ 
				var date = dp.cal.getNewDateStr();            
				BookingRmk(date);//调用其它函数并带上参数                
			}
			
			function BookingRmk(date){         
				tt = date;
				var date1 = new Date(tt);
				var date2 = new Date(dt);							
				k = 0;
				if(date1.getTime() < date2.getTime()){			
					k = 1;
					tradeday();
				}								
				start();						
			}

			function prev(){  //前一天
				pn = -1;
				var day = new Date(tt);	
				day.setTime(day.getTime() - 24*60*60*1000);	
				tt = day.Format("yyyy-MM-dd");
				BookingRmk(tt);
			}
			
			function next(){  //后一天 				
				pn = 1;
				var day = new Date(tt);	
				day.setTime(day.getTime() + 24*60*60*1000);	
				tt = day.Format("yyyy-MM-dd");
				BookingRmk(tt);			
			}
			
			var pn = -1;
			function tradeday() {  //判断最后交易日
				var day = new Date(tt);							
				for (i = 1; i < 100; i++){	//节假日																										
					if(i > 1){
						day.setTime(day.getTime() + pn*24*60*60*1000);							
					}	
					tt = day.Format("yyyy-MM-dd");
					//alert(tt);
					if(tt == dt){	
						k = 0;						
						break;
					}
					var url = "http://127.0.0.1:8881/w1/api/index.php?Order=0&a=DailyLimitPerformance&st=1000&apiv=w26&Type=4&c=HisHomeDingPan&Index=0&PidType=1&Day=" + tt;				

					var res = [];
					$.ajax({
						type: "get",
						url: url,
						dataType: "json",
						async : false,
						cache:false,
						timeout:3000,						
						success: function(data) {
							res = data.info;							
						}
					});
					if(res[0].length != 0){						
						break;
					}
				}
			}
			
			
			var datas = "";
			var gl = "涨停时间,涨幅";  //高亮
			function start() {
				datas = "";
				var div = document.getElementById("title");										
				div.innerHTML = '<font style="margin-left:20px;" size="3">连板&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><input style="width: 120px;" type="text" id="PriceDate" name="PriceDate" class="Wdate" onFocus="WdatePicker({ onpicking: cDayFunc})" />';						
				var back = '<a href="javascript:prev();"  style="padding-left:10px;font-size:16px;color:#fff;">←</a>';				
				var forward = '<a href="javascript:next();"  style="font-size:16px;color:#fff;">→</a>';
				div.innerHTML += back + forward;
				
				for (i = 0; i < 5 ; i++){
					getSource(5 - i);					
				}
				
				document.getElementById("PriceDate").value = tt;
				
				for (i = 1; i < 6 ; i++){
					var table = document.getElementById("已涨停" + i);
					if(table){
						makeSortable(table);						
					}
				}	
				
				for (i = 1; i < 5 ; i++){
					var table = document.getElementById("未涨停" + i);
					if(table){
						makeSortable(table);
					}
				}
				hover();
			}
			
			
			function getSource(ss) {
				//alert(ss);
				q = ss;
				var urla1="http://127.0.0.1:8880/w1/api/index.php?Order=0&a=DailyLimitPerformance&st=60&apiv=w21&Type=4&c=HomeDingPan&PidType=" + ss;
				var urla2 = "http://127.0.0.1:8881/w1/api/index.php?Order=0&a=DailyLimitPerformance&st=1000&apiv=w26&Type=4&c=HisHomeDingPan&Index=0&PidType=" + ss + "&Day=" + tt;
				
				var urlb1 = "http://127.0.0.1:8880/w1/api/index.php?Order=1&a=DailyLimitPerformance2&st=1000&apiv=w21&Type=5&c=HomeDingPan&PidType=" + ss;
				var urlb2 = "http://127.0.0.1:8881/w1/api/index.php?Order=1&a=DailyLimitPerformance2&st=1000&apiv=w26&Type=5&c=HisHomeDingPan&PidType=" + ss + "&Day=" + tt;
				
				var bta = "";
				var btb = "";
				var urla = urla1;
				var urlb = urlb1;
				if(k == "1"){
				  urla = urla2;
				  urlb = urlb2;
				}
				var ida = "";
				var idb = "";
				if(ss == "5"){										
					bta = "四板以上：";
					btb = "未涨停昨日四板及以上个股：";
					ida = "towritea4";
					idb = "towriteb4"
				}else if(ss == "4"){					
					bta = "四板：";
					btb = "未涨停昨日三板个股：";
					ida = "towritea3";
					idb = "towriteb3"
				}else if(ss == "3"){					
					bta = "三板：";
					btb = "未涨停昨日二板个股：";
					ida = "towritea2";
					idb = "towriteb2"
				}else if(ss == "2"){					
					bta = "二板：";
					btb = "未涨停昨日一板个股：";
					ida = "towritea1";
					idb = "towriteb1"
				}else if(ss == "1"){					
					bta = "一板：";
					ida = "towrites";
				}				
				
				var res1 = [];
				$.ajax({
					type: "get",
					url: urla,
					dataType: "json",
					async : false,
					cache:false,
					timeout:3000,						
					success: function(data) {
						var arr = data.info;
						if(ss == 1 && k == 0){
							tt = arr[1];
							dt = tt;
						}						
						var arry = arr[0];						
						if(arry.length > 0){
							for (j = 0; j < arry.length; j++){
								res1.push({"code":arry[j][0],"name":arry[j][1],"zttime":getLocalTime(arry[j][4]),"je":NumberTransform(arry[j][8]),"ztyy":arry[j][5],"lb":arry[j][18]});							
							}
						}
						//alert(JSON.stringify(res1))
					}
				});
				tables(res1,bta,ida);
								
				if(ss != "1"){					
					res2 = [];
					$.ajax({
						type: "get",
						url: urlb,
						dataType: "json",
						async : false,
						cache:false,
						timeout:3000,						
						success: function(data) {
							var arr = data.info;
							var arry = arr[0];
							if(arry.length > 0){
								for (j = 0; j < arry.length; j++){							
									res2.push({"code":arry[j][0],"name":arry[j][1],"xj":arry[j][4],"zf":arry[j][5],"je":NumberTransform(arry[j][8]),"zt":arry[j][6],"lb":arry[j][15]});							
								}
							}							
						}
					});
					tables(res2,btb,idb);					
				}									
			}
			
			var q = 0;
			var bj = [];
			var bja = [];
			var bjb = [];
			function tables(data,bt,id) {			
				if(data != ""){
					//document.write(data);
					//var json = JSON.parse(data);
					var json = data;
					//创建table					
					var table=document.createElement("table");										
					var thead=document.createElement("thead");
					table.appendChild(thead);					
					var tr=document.createElement("tr");
					thead.appendChild(tr);					
					
					var m = 0;
					var v = 0;
					var b = 0;
					bj = [];
					for(var key in json[0]){  //表头													
						key = key.replace('code','代码').replace('name','名称').replace('zttime','涨停时间').replace('ztyy','涨停原因').replace('je','主力净额');
						key = key.replace('xj','现价').replace('zf','涨幅%').replace('fde','封单额').replace('zt','主题').replace('lb','连板');
						
						var th=document.createElement("th");						
						th.innerHTML=key;						
						tr.appendChild(th);
						if(gl.indexOf(key.replace("%","")) > -1){
							v = m;
							th.style.color = "#FF6633";	
						}
						if(key == "涨停原因" || key == "主题" || key == "连板"){												
							bj.push(m);
							th.style.color = "#FFA500";	
							if(key == "涨停原因"){
								b = m;
							}	
						}
						m++;
					}
										
					var tbody=document.createElement("tbody");					
					table.appendChild(tbody);
					
					//遍历json
					for(var i = 0; i < json.length; i++){					
						var tr=document.createElement("tr");						
						var p = 1;
						var dm = "";
						//var mc = "";
						var ztyy = "涨停原因";						
						for(var key in json[i]){ //行元素遍历					
							var td=document.createElement("td");														
							var rc = json[i][key];
							if(p == 1){  //代码列
								dm = rc;
								//tr.id = dm;
							}
							if(p == b + 1){
								ztyy = rc;
								tr.id = dm + "#" + ztyy;
							}
							td.innerHTML = "<a href=http://www.treeid/code_" + dm + " id='" + p + "#" + ztyy + "' onclick='changeCss(this,\"" + dm + "\",\"towrite\")'>" + rc + "</a>";
							if(p == v + 1){							
								//td.style.color = "#FF6633";
								td.getElementsByTagName("a")[0].style.color = "#FF6633";								
							}
							if(bj.indexOf(p - 1) > -1){	 //需要BJ的列
								td.getElementsByTagName("a")[0].style.color = "#FFA500"; 
							}							
							tr.appendChild(td);
							p++;
						}
						tr.innerHTML = tr.innerHTML.replace(/涨停原因/g,ztyy);						
						tbody.appendChild(tr);
					}
					
					var div = document.getElementById(id);
					var bts = '<p style="margin:2px;color:#FFFF00;font-size:14px">' + bt + '(' + json.length + ')' + '</p>';
					if(bt.indexOf("未涨停") > -1){
						bts = '<p style="margin:2px;color:#7CFC00;font-size:14px">' + bt + '(' + json.length + ')' + '</p>';
					}
					div.innerHTML = bts;
					div.appendChild(table);	
					//changeTable(table,"0#4");
					if(bt.indexOf("未涨停") > -1){					
						table.id = "未涨停" + q;
						bjb = bj;				
					}else{
						table.id = "已涨停" + q;
						bja = bj;
					}					
				}else{
					var div = document.getElementById(id);
					var bts = '<p style="margin:2px;color:#FFFF00;font-size:14px">' + bt + '(0)' + '</p>';
					if(bt.indexOf("未涨停") > -1){
						bts = '<p style="margin:2px;color:#7CFC00;font-size:14px">' + bt + '(0)' + '</p>';
					}
					div.innerHTML = bts;
				}				
			}
			
			//隐藏某列				
            function changeTable(table,ss) {	
				var trs = table.rows;
				ss = ss + '#';
				var aa = ss.split('#');
				for (var j = 0; j < aa.length; j++) {
					if(aa[j] != ""){
						for (var i = 0, len = trs.length; i < len; i++) {					
							var p = aa[j];
							var cell = trs[i].cells[p]; 
							cell.style.display = 'none';
						}
					}
				}					
			}
			
			var trid = "";
			var ty = "";			
			function changeCss(obj,hid,id){
				ty = obj.id.split("#")[1];				
				var tlist = document.getElementById(id).getElementsByTagName("tr");					
				for(var i = 0; i < tlist.length; i++){
					var dm = tlist[i].id.split("#")[0];
					var yy = tlist[i].id.split("#")[1];
					if(yy == ty && ty != ""){
						if(dm == hid){
							tlist[i].style.backgroundColor = "#483D8B";
							trid = hid;
						}else{
							tlist[i].style.backgroundColor = "#800080"; //相同涨停原因
						}
					}else{
						tlist[i].style.backgroundColor = bkcolor; 
					}
				}
			}			
			
			function hover(){  //鼠标经过
				var rows = document.getElementById("towrite").getElementsByTagName('tr');
				for(var i=0;i<rows.length;i++){
					rows[i].onmouseover=function(){			
						if(this.id != ""){  //过滤表头
							this.style.background="#800000";
						}
					}
					rows[i].onmouseout=function(){
						if(this.id != ""){  //过滤表头						
							var dm = this.id.split("#")[0];
							var yy = this.id.split("#")[1];
							if(dm == trid){
								this.style.background="#483D8B";
							}else if(yy == ty){								
								this.style.background="#800080";														
							}else{
								this.style.background=bkcolor;
							}
							
						}						
					}					
				}				
			}
			
			function makeSortable(table) {			
				var headers=table.getElementsByTagName("th");
				for(var i=0;i<headers.length;i++){				
					(function(n){
						var flag=false;
							
						headers[n].onclick=function(){
													
							var bj = [];
							if(table.id.indexOf("已涨停")>-1){
								bj = bja;
							}else{
								bj = bjb;
							}							
							showCol(table,n,bj);		//高亮
							var tbody=table.tBodies[0];
							var rows=tbody.getElementsByTagName("tr");
							rows=Array.prototype.slice.call(rows,0);

							//基于第n个<td>元素的值对行排序
							rows.sort(function(row1,row2){
								headtxt = headers[n].textContent;								
								var cell1=row1.getElementsByTagName("td")[n];//获得第n个单元格
								var cell2=row2.getElementsByTagName("td")[n];																
								var val1=cell1.textContent||cell1.innerText;//获得文本内容
								var val2=cell2.textContent||cell2.innerText;
								if(headtxt.indexOf("名称") != -1 || headtxt.indexOf("连板") != -1 || headtxt.indexOf("主题") != -1 || headtxt.indexOf("涨停时间") != -1 || headtxt.indexOf("涨停原因") != -1){
									val1 = val1;
									val2 = val2;
								}else{
									if(val1 == "-"){
										val1 = " ";
									}
									if(val2 == "-"){
										val2 = " ";
									}									
									if(val1.indexOf("万") != -1){
										val1 = parseFloat(val1)*10000;
									}else if(val1.indexOf("亿") != -1){
										val1 = parseFloat(val1)*100000000;
									}else{
										val1 = parseFloat(val1);
									}
									
									if(val2.indexOf("万") != -1){
										val2 = parseFloat(val2)*10000;
									}else if(val2.indexOf("亿") != -1){
										val2 = parseFloat(val2)*100000000;
									}else{
										val2 = parseFloat(val2);
									}
								}
								if(val1<val2){
									return -1;
								}else if(val1>val2){
									return 1;
								}else{
									return 0;
								}
							});
							if(flag){
								rows.reverse();
							}
							
							for(var i=0;i<rows.length;i++){
								tbody.appendChild(rows[i]);
							}

							flag=!flag;
						}
					}(i));
				}
			}
			
			function showCol(table,iCol,arr){	 //(选中列，标记列)	
			//alert(JSON.stringify(arr))	
				//var oTable = document.getElementsByTagName('table')[0];
				var oTable = table;						
				//表头
				var th = oTable.getElementsByTagName("th");						
				for (var i=0;i < th.length; i++){	    //列遍历
					th[i].style.color = "#E5E5E5";      //还原					
					if(arr.length > 0 && arr.indexOf(i) > -1){  //BJ列高亮
						th[i].style.color = "#FFA500";
					}
					th[iCol].style.color = "#FFFF80";	//选中				
				}
				
				var tr = oTable.getElementsByTagName("tr");	
				for (var i=1;i < tr.length; i++){	//行遍历
					var otagsA = tr[i].getElementsByTagName("a");
					for (var j=0;j < otagsA.length; j++){	//列遍历
						otagsA[j].style.color = "#E5E5E5";	//还原																
						if(arr.length > 0 && arr.indexOf(j) > -1){  //BJ列高亮
							otagsA[j].style.color = "#FFA500";
						}
					}
					var value = otagsA[iCol].innerText.replace(/(万)|(亿)/g,'');
					if(!isNaN(parseFloat(value)) && isFinite(value)){  //判断列是否为数字
						if(parseFloat(value) >= 0){
							otagsA[iCol].style.color = "#FF6633";     //正负颜色
						}else{
							otagsA[iCol].style.color = "#00FF00";
						}
					}else{
						otagsA[iCol].style.color = "#FF6633";
					}
					
				}					
			}	
			
		</script>
	</body>	
</html>